#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QtDeclarative>

#include <mycallback.h>
#include <mainwindow.h>

#include <hardware/emocap/sbs2emocapdatareader.h>
#include <hardware/emotiv/sbs2emotivdatareader.h>
#include <hardware/emocap28/sbs2emocap28datareader.h>

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    //set path where recorded data will be stored
    qDebug() << "catalogPath: "<<Sbs2Common::setDefaultCatalogPath();
    //set path where application data is stored
    qDebug() << "rootAppPath: "<<Sbs2Common::setDefaultRootAppPath();

    Sbs2Common::setHardware("emocap28");

    //create MyCallback object
    MyCallback* myCallback = new MyCallback();
    //create Sbs2DataReader; 0 indicates that we do not overclock N900 (default)
    Sbs2Emocap28DataReader* sbs2DataReader = Sbs2Emocap28DataReader::New(myCallback,0);

    //a
    MainWindow* mw = new MainWindow(myCallback);
    mw->setAttribute(Qt::WA_QuitOnClose);

    QObject::connect(app.data(),SIGNAL(aboutToQuit()),mw->glwidget,SLOT(kill()));
    QObject::connect(app.data(), SIGNAL(aboutToQuit()), sbs2DataReader, SLOT(aboutToQuit()));
    QObject::connect(myCallback,SIGNAL(valueSignal(int)),mw->glwidget,SLOT(update(int)));
    QObject::connect(myCallback,SIGNAL(gyroSignal(int,int)),mw->glwidget,SLOT(gyroSlot(int,int)));
    QObject::connect(mw->glwidget,SIGNAL(turnFilterOn(int,int,int)),myCallback,SLOT(turnFilterOn(int,int,int)));
    QObject::connect(mw->glwidget,SIGNAL(turnFilterOff()),myCallback,SLOT(turnFilterOff()));
    QObject::connect(mw->glwidget,SIGNAL(turnSpectrogramOn(int,int,int)),myCallback,SLOT(turnChannelSpectrogramOn(int,int,int)));
    QObject::connect(mw->glwidget,SIGNAL(turnSpectrogramOff()),myCallback,SLOT(turnChannelSpectrogramOff()));
    QObject::connect(myCallback,SIGNAL(spectrogramUpdated()),mw->glwidget,SLOT(updateSpectro()));
    //b

    //   QmlApplicationViewer viewer;
    //   viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    //   viewer.setMainQmlFile(QLatin1String("qml/sbs2emocap28/main.qml"));
    //   viewer.showExpanded();

    //  QObject *rootObject = dynamic_cast<QObject*>(viewer.rootObject());

    // QObject::connect(sbs2DataReader,SIGNAL(amp1FoundSignal(QVariant,QVariant,QVariant)),rootObject,SLOT(amp1FoundSignal(QVariant,QVariant,QVariant)));
    // QObject::connect(sbs2DataReader,SIGNAL(amp2FoundSignal(QVariant,QVariant,QVariant)),rootObject,SLOT(amp2FoundSignal(QVariant,QVariant,QVariant)));
    //  QObject::connect(app.data(), SIGNAL(aboutToQuit()), sbs2DataReader, SLOT(aboutToQuit()));
    //  QObject::connect((QObject*)viewer.engine(), SIGNAL(quit()), app.data(), SLOT(quit()));

    //c

    return app->exec();
}
